﻿//-------------------------------------------------------------------------------------
//-------md5文件校验 ------------------------------------------------------------
//-------md5.h----------------------------------------------------------------------
//-------2021/07/2---by mky.蒙克雨-------------------------------------------
//--------------------------------------------------------------------------------------
#ifndef _MD5_H
#define _MD5_H

//--------------------------------------------------------------------------------------
#include <string>
#include <string.h>
//--------------------------------------------------------------------------------------
using namespace std;

//--------------------------------------------------------------------------------------
struct MD5_DATA
{
 unsigned long data[4];
 bool operator < (const MD5_DATA& p) const
 {
  return memcmp(data,p.data,4*sizeof(long))>0;
 }
};

//--------------------------------------------------------------------------------------
namespace mky
{
    class MD5
    {
        //--------------------------------------------------------------------------------------
        private:
        //--------------------------------------------------------------------------------------
            #define uint8  unsigned char
            #define uint32 unsigned long int
            #define  MD5_DIGEST_LENGTH    1024 * 16
            struct md5_context
            {
            uint32 total[2];
            uint32 state[4];
            uint8 buffer[64];
            };
            void md5_starts( struct md5_context *ctx );
            void md5_process( struct md5_context *ctx, uint8 data[64] );
            void md5_update( struct md5_context *ctx, uint8 *input, size_t length );
            void md5_finish( struct md5_context *ctx, uint8 digest[16] );

        //--------------------------------------------------------------------------------------
        public:
        //--------------------------------------------------------------------------------------
            //!从任意的fileNmae中构建MD5
            int GenerateMD5(const char* filename);
            //!从任意的bufer中构建MD5
            void GenerateMD5(unsigned char* buffer,size_t bufferlen);
            //! to str md5
            string md5(string str);
            //! construct a MD5
            MD5();

            //! construct a md5src from char *
            MD5(const char * md5src);

            //! construct a MD5 from a 16 bytes md5
            MD5(unsigned long* md5src);

            //! add a other md5
            MD5 operator +(MD5 adder);

            //! just if equal
            bool operator ==(MD5 cmper);

            //! give the value from equer
            // void operator =(MD5 equer);

            //! to a string
            string ToString();

            unsigned long m_data[4];
    //--------------------------------------------------------------------------------------
    };
}

//--------------------------------------------------------------------------------------
#endif//!_MD5_H